package org.hackystat.devcathlon.engine.event;

import javax.xml.datatype.XMLGregorianCalendar;
import org.hackystat.devcathlon.engine.db.util.HibernateUtil;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.user.User;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 * Event Score Manager for dealing with computations of event scores.
 * 
 * @author Anthony Du
 *
 */
public class EventScoreManager {
  /** The singleton instance of this class. */
  private static EventScoreManager theInstance = new EventScoreManager();
  
  /** The private constructor. */
  private EventScoreManager() {
    // empty constructor
  }

  /**
   * Returns the singleton instance of the EventManager.
   * 
   * @return The EventManager.
   */
  public static EventScoreManager getInstance() {
    return EventScoreManager.theInstance;
  }
  
  /**
   * Adds an event score to the system.
   * 
   * @param timestamp The timestamp.
   * @param match The match.
   * @param team The team.
   * @return The event score.
   */
  public synchronized EventScore addEventScore(XMLGregorianCalendar timestamp, Match match, Team team) {
    EventScore eventScore = new EventScore(timestamp, match, team);
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {
      session.beginTransaction();
      session.save(eventScore);
      session.getTransaction().commit();
    }
    catch (HibernateException ex) {
      Transaction tx = session.getTransaction();
      if (tx.isActive()) {
        tx.rollback();
      }
      throw ex;
    }
    return eventScore;
  }
  
  /**
   * Adds an event score to the system.
   * 
   * @param timestamp The timestamp
   * @param match The match.
   * @param team The team.
   * @param user The user.
   * @return The event score.
   */
  public synchronized EventScore addEventScore(XMLGregorianCalendar timestamp, Match match, 
      Team team, User user) {
    EventScore eventScore = new EventScore(timestamp, match, team, user);
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {
      session.beginTransaction();
      session.save(eventScore);
      session.getTransaction().commit();
    }
    catch (HibernateException ex) {
      Transaction tx = session.getTransaction();
      if (tx.isActive()) {
        tx.rollback();
      }
      throw ex;
    }
    return eventScore;
  }
  
  /**
   * Updates a given event score.
   * 
   * @param eventScore The event score.
   * @return The updated event score. 
   */
  public synchronized EventScore updateEventScore(EventScore eventScore) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    try {
      session.beginTransaction();
      session.saveOrUpdate(eventScore);
      session.getTransaction().commit();
    }
    catch (HibernateException ex) {
      Transaction tx = session.getTransaction();
      if (tx.isActive()) {
        tx.rollback();
      }
      throw ex;
    }
    return eventScore;
  }
  
  /**
   * Gets the calculated event score.
   * 
   */
}
